﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Autoverwaltung.Datenhaltung
{
    public class AutoverwaltungModelCollection<T> where T : Model.IAutoverwaltungModel
    {
        private int lastId = -1;
        private List<T> elements = new List<T>();

        public int NextId
        {
            get { return this.lastId + 1; }
        }

        public bool Add(T element)
        {
            foreach (T elem in this.elements)
            {
                if (elem.Id.Equals(element.Id))
                {
                    return false;
                }
            }

            if (this.lastId < element.Id)
            {
                this.lastId = element.Id;
            }
            this.elements.Add(element);

            return true;
        }

        public T GetById(int id)
        {
            foreach (T element in this.elements)
            {
                if (element.Id.Equals(id))
                {
                    return element;
                }
            }

            return default(T);
        }


        public bool Remove(int id)
        {
            for (int i = 0; i < this.elements.Count; i++)
            {
                if (this.elements[i].Id.Equals(id))
                {
                    this.elements.RemoveAt(i);
                    return true;
                }
            }

            return false;
        }

        public bool Remove(T element)
        {
            return this.elements.Remove(element);
        }

        public T[] ToArray()
        {
            return this.elements.ToArray();
        }
    }
}
